<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>{{$config['modelShowName']}}</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search" id="search">
                    <div class="layui-form-item">
                        <!--
                        <div class="layui-inline">
                            <label class="layui-form-label">ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="id" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        -->
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="current-table" lay-filter="current-table"></table>

    </div>
</div>
<script type="text/html" id="topToolBar">
    <div class="layui-btn-container">
        @if(in_array('insert',$functions))
            <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="insert" auth="{{lcfirst($config['modelName'])}}:insert">
                <i class="layui-icon layui-icon-addition"></i>添加</button>
        @endif
        @if(in_array('import',$functions))
            <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="import" auth="{{lcfirst($config['modelName'])}}:import">导入</button>
        @endif
        @if(in_array('export',$functions))
            <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="export" auth="{{lcfirst($config['modelName'])}}:export">
                <i class="layui-icon layui-icon-export"></i>导出</button>
        @endif
        @if(in_array('batchDelete',$functions))
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchDelete" auth="{{lcfirst($config['modelName'])}}:delete">
                <i class="layui-icon layui-icon-delete"></i>批量删除</button>
        @endif
    </div>
</script>
<script type="text/html" id="currentTableBar">
    @if(in_array('update',$functions))
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="update" auth="{{lcfirst($config['modelName'])}}:update">编辑</a>
    @endif
    @if(in_array('delete',$functions))
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete" auth="{{lcfirst($config['modelName'])}}:delete"><i class="layui-icon layui-icon-delete"></i>删除</a>
    @endif
</script>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=4" charset="utf-8"></script>
<script>
new Vue({
    el: "#search",
    data: {
        services: JSON.parse(localStorage.getItem('services')),
    },
    created() {

    },
    updated() {

    },
    mounted() {

    }
})
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        table.render({
            elem: '#current-table',
            url: "/admin/{{lcfirst($config['modelName'])}}/paginate",
            toolbar: '#topToolBar',
            defaultToolbar: ['filter'],
            cols: [[
                {type: "checkbox", width: 50},
@foreach($config['fields'] as $index=>$item)
@if(in_array($item['type'],['select','radio']))
                {field: "{{$item['field']}}", title: "{{$item['title']}}",templet:function(row){
                        var values = {!! json_encode($item['values'],JSON_UNESCAPED_UNICODE) !!};
                        var value = row.{{$item['field']}};
                        for(var k in values){
                            if(value === values[k].value ){
                                return values[k].title
                            }
                        }
                        return row.{{$item['field']}};
                    }},
@else
                {field: "{{$item['field']}}", title: "{{$item['title']}}"},
@endif
@endforeach
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });
        form.on('submit(data-search-btn)', function (data) {
            var searchData = data.field;
            //执行搜索重载
            table.reload('current-table', {
                page: { curr : 1 }
                , where:searchData
            }, 'data');
            return false;
        });
        table.on('toolbar(current-table)', function (obj) {
            if (obj.event === 'insert') {
                layer.open({
                    title: "{{$config['modelShowName']}}-添加",
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['700px', '500px'],
                    content: 'form.html',
                });
@if(in_array('import',$functions))
            } else if(obj.event==='import'){
                console.log('import')
@endif
@if(in_array('export',$functions))
            } else if(obj.event==='export'){
                console.log('export')
@endif
@if(in_array('batchDelete',$functions))
            } else if(obj.event==='batchDelete'){
                var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
                var data = checkStatus.data;  // 获取选中行数据
                if (data.length===0) return;
                var deleteNum = 0;
                var delPop = layer.confirm('确定要批量删除吗?',{title:"提示",icon:3},function(){
                    for(var k in data){
                        var id = data[k].id;
                        $.post("/admin/{{lcfirst($config['modelName'])}}/" + id,{_method:'delete'},function (res) {
                            if (res.code === 0){
                                deleteNum++
                            }
                        });
                    }
                    var interval =setInterval(function () { //判断删除数量
                        if (deleteNum===data.length){ //删除完毕
                            layer.msg('删除成功',{icon:1,time:500},function(){
                                table.reload('current-table');
                            })
                            layer.close(delPop)
                            table.reload()
                            clearInterval(interval)
                        }
                    },10)
                })

@endif
            } else {
                console.log(1)
            }
        });

        table.on('tool(current-table)', function (obj) {
            if (obj.event === 'update') {
                layer.open({
                    title: "{{$config['modelShowName']}}-编辑",
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['700px', '500px'],
                    content: 'form.html?id='+obj.data.id,
                });
                return false;
            } else if (obj.event === 'delete') {
                var delPop = layer.confirm('确定要删除吗?',{title:"提示",icon:3},function(){
                    $.post("/admin/{{lcfirst($config['modelName'])}}/"+obj.data.id,{_method:'delete'},function (res) {
                        if (res.code === 0){
                            layer.msg('删除成功',{icon:1,time:500},function(){
                                table.reload('current-table');
                            })
                            table.reload()
                            layer.close(delPop)
                        } else {
                            layer.msg(res.msg,{icon:2})
                        }
                    });
                })
            }
        });
    });
</script>
</body>
</html>
